package cn.vendsystem.com.mapper;


import cn.vendcommon.com.entity.InventoryItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface InventoryMapper {

    // 新增商品
    @Insert("INSERT INTO inventory_item(name, count, max, machine_id, position, price, status, picture) " +
            "VALUES(#{name}, #{count}, #{max}, #{machineId}, #{position}, #{price}, #{status}, #{picture})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(InventoryItem item);

    // 删除商品
    @Delete("DELETE FROM inventory_item WHERE id = #{id}")
    int deleteById(int id);

    // 修改商品
    @Update("UPDATE inventory_item SET name=#{name}, count=#{count}, max=#{max}, machine_id=#{machineId}, " +
            "position=#{position}, price=#{price}, status=#{status}, picture=#{picture} WHERE id=#{id}")
    int update(InventoryItem item);

    // 根据ID查询商品
    @Select("SELECT * FROM inventory_item WHERE id = #{id}")
    InventoryItem selectById(int id);

    // 查询所有商品
    @Select("SELECT * FROM inventory_item")
    List<InventoryItem> selectAll();

    // 根据售卖机ID查询商品
    @Select("SELECT * FROM inventory_item WHERE machine_id = #{machineId}")
    List<InventoryItem> selectInventoryByMachineId(int machineId);

    // 更新商品图片
    @Update("UPDATE inventory_item SET picture = #{picture} WHERE id = #{id}")
    void updatePictureById(InventoryItem inventory);

    @Select("select picture from inventory_item where id = #{id}")
    String getPictureById(Integer id);

    void updateAll(@Param("inventoryItems") List<InventoryItem> inventoryItems, @Param("machineId") int machineId);

}
